<?php 

function translate_word($key,$dictionary){
	if(array_key_exists($key,$dictionary)){
		return $dictionary[$key];
	}else{
		return "<a target='_blank' href='http://".SERVER_PATH."admin/translate-word.html?word={$key}'>tradu cuvantul {$key}</a>";
	}
} 
function get_dictionary($lang){
	global $userObj;
	$params=array($lang);
	
	$row = $userObj->fetch_result("SELECT keyword,text FROM translates WHERE language_id=?",$params);
	foreach($row as $key=>$value){
		$dic[$value['keyword']]=$value['text'];
	}
	return $dic;
}
function get_dictionary_shop($lang)
	{
		global $shopObj; 
		$params=array($lang);
	 	$values = $shopObj->fetch_result("SELECT * FROM translates WHERE language_id = ?",$params);
		
		$translations = array();
		foreach($values as $key => $value)
		{
			$translations[$value["keyword"]] = $value["text"];
		}
		
		return $translations;
	}
	
function translate_word_shop($name)
{
	
	global $dictionary;
	if(array_key_exists($name,$dictionary)){
		return $dictionary[$name];
	}else{
		return "<a style='color:pink;text-transform:uppercase; font-weight:600;' target='_blank' rel='nofollow' href='http://".SERVER_PATH."admin/translate-word.html?keyword=".$name."'>translate</a>";
	}
} 

function createURL($tableName, $name, $obj, $index = 1, $count = 0)

{

	if($index == 1)

	{

		$url = sanitizeString($name);

	}

	else

	{

		$url = $name;

	}
	$params=array($url);
	$duplicateURL = $obj->fetch_result("SELECT t.url FROM {$tableName} t WHERE t.url = ?",$params);

	if(count($duplicateURL[0]) > $count)

	{

		$url = $url."_".$index;

		$index++;

		createURL($tableName, $url,$obj, $index);

	}

	return $url;

}

function sanitizeString($string) {

//Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )

	$string = strtolower($string);

//Strip any unwanted characters

	$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);

//Clean multiple dashes or whitespaces

	$string = preg_replace("/[\s-]+/", " ", $string);

//Convert whitespaces and underscore to dash

	$string = preg_replace("/[\s_]/", "-", $string);

	return $string;

}
function generateCouponCode($obj)
{
    $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $res = "";
    for ($i = 0; $i < 15; $i++)
    {

    $res .= $chars[mt_rand(0, strlen($chars)-1)];

    }

     

    $count =$obj->num_rows("SELECT * FROM discounts WHERE coupon_code = '{$res}'"); 

    if( $count > 0)

    {

    $res = generateCouponCode();

    }

     

    return $res;

}
// function validateEmail($email) {
// 	  // First, we check that there's one @ symbol, 
// 	  // and that the lengths are right.
// 	  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// 	    // Email invalid because wrong number of characters 
// 	    // in one section or wrong number of @ symbols.
// 	    return false;
// 	  }
// 	  // Split it into sections to make life easier
// 	  $email_array = explode("@", $email);
// 	  $local_array = explode(".", $email_array[0]);
// 	  for ($i = 0; $i < sizeof($local_array); $i++) {
// 	    if
// 	(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
// 	↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
// 	$local_array[$i])) {
// 	      return false;
// 	    }
// 	  }
// 	  // Check if domain is IP. If not, 
// 	  // it should be valid domain name
// 	  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
// 	    $domain_array = explode(".", $email_array[1]);
// 	    if (sizeof($domain_array) < 2) {
// 	        return false; // Not enough parts to domain
// 	    }
// 	    for ($i = 0; $i < sizeof($domain_array); $i++) {
// 	      if
// 	(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
// 	↪([A-Za-z0-9]+))$",
// 	$domain_array[$i])) {
// 	        return false;
// 	      }
// 	    }
// 	  }
// 	  return true;
// 	}

?>